import java.util.List;
import java.util.ArrayList;

public class problem216 {
//    public static void main(String[] args) {
//        Long number = 2L;
//        Long value;
//        int howManyPrimes = 0;
//        do {
//            value = (((Double) Math.pow(number, 2)).longValue() * 2) - 1;
//
//            if (ProjectEuler.isPrime(value))
//                howManyPrimes++;
//
//            number++;
//
//            if (number % 10000 == 0)
//                System.out.println("Reached " + number);
//        } while (number <= 50000000);
////        } while (number <= 10000);
//
//        System.out.println(howManyPrimes);
//    }
    private static Long numberOfPrimes = 0L;

    public static boolean anyAlive(List<PrimeThread> threads) {
        boolean anyAlive = false;
        for (PrimeThread thread : threads)
            if (thread.isAlive()) {
                anyAlive = true;
                break;
            } else {
                threads.remove(thread);
            }
        return anyAlive;
    }

    public static void main(String[] args) throws Exception {
        Long maxNumber = 50000000L;
        Long numbersPerThread = 5000000L;
//        Long maxNumber = 5000000L;
        List<PrimeThread> threads = new ArrayList<PrimeThread>();

        Long numbersLeft = maxNumber;
        Long lastNumber;
        while(numbersLeft != 0L) {
            if (numbersLeft % numbersPerThread != numbersLeft) {
                lastNumber = numbersLeft;
                numbersLeft -= numbersPerThread;
            } else {
                lastNumber = numbersLeft;
                numbersLeft = 0L;
            }

            PrimeThread primeThread = new PrimeThread(numbersLeft + 1, lastNumber);
            primeThread.start();
            threads.add(primeThread);
        }

        while(anyAlive(threads)) {
            Thread mainThread = Thread.currentThread();
//            System.out.println(mainThread + " going to sleep");
            mainThread.sleep(60000);
        }

        for(PrimeThread thread : threads) {
            numberOfPrimes += thread.getSum();
        }

        System.out.println(numberOfPrimes);
    }
}
